package com.ternnetwork.oa.config;


import org.quartz.Scheduler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;

import java.util.Properties;


/**
 * 调度工厂配置
 * @author xuwenfeng
 *
 */
@Configuration
public class SchedulerFactoryBeanConfiguration {
	

	@Autowired
    @Qualifier(value = "dataSource")
    private DataSource dataSource;
	@Autowired
	private JobFactory jobFactory;
	

	@Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("quartz.properties"));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

	
	@Bean
	public SchedulerFactoryBean schedulerFactoryBean() throws IOException{
		
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();

		schedulerFactoryBean.setQuartzProperties(quartzProperties());
		
		schedulerFactoryBean.setDataSource(dataSource);

		schedulerFactoryBean.setJobFactory(jobFactory);
		
		return schedulerFactoryBean;
	}

	@Bean(name="scheduler")
	public Scheduler scheduler() throws IOException {
		return schedulerFactoryBean().getScheduler();
	}


}
